package jsr166.contrib.uncontended;

import java.util.concurrent.atomic.AtomicReference;

 class AbstractOwnedUncontendedLock extends AbstractUncontendedLock {

    protected volatile boolean broken;
    void setOwningThread(AtomicReference<Thread> threadHoldingLock) {
        Thread owner = threadHoldingLock.get();
        boolean amOwner;
        if (owner == null) {
            do {
                amOwner = threadHoldingLock.compareAndSet(null, Thread.currentThread());
                if (!amOwner) owner = threadHoldingLock.get(); 
            } while (!amOwner && owner == null);
        } else
            amOwner = owner == Thread.currentThread();
        if (!amOwner) 
            lockWasContended(owner);
    }

    protected void lockWasContended(Thread owner) {
        broken = true;
        throw new UnexpectedConcurrentAccessException("Lock is contended, is held by ", owner);
    }
    protected void lockWasContended() {
        broken = true;
        throw new UnexpectedConcurrentAccessException("Lock was contended");
    }
    protected void lockAlreadyBroken() {
        throw new UnexpectedConcurrentAccessException("Previous uses of this lock were contended");
    }

    public void lock() {
        // TODO Auto-generated method stub

    }

    public void unlock() {
        // TODO Auto-generated method stub

    }

}
